package cn.cloudreal.cbms.project.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import cn.cloudreal.cbms.common.exception.ITCException;
import cn.cloudreal.cbms.common.util.Arith;
import cn.cloudreal.cbms.common.util.Constants;
import cn.cloudreal.cbms.common.util.DatadicContext;
import cn.cloudreal.cbms.common.util.PageBean;
import cn.cloudreal.cbms.common.util.StringUtils;
import cn.cloudreal.cbms.log.service.OptLogService;
import cn.cloudreal.cbms.medium.service.MediumService;
import cn.cloudreal.cbms.project.service.ProjectService;
import cn.cloudreal.cbms.system.service.OrganizaService;
import cn.cloudreal.cbms.vo.CastForProj;
import cn.cloudreal.cbms.vo.Dictionary;
import cn.cloudreal.cbms.vo.Log;
import cn.cloudreal.cbms.vo.Medium;
import cn.cloudreal.cbms.vo.Organiza;
import cn.cloudreal.cbms.vo.Project;
import cn.cloudreal.cbms.vo.UserInfo;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * <p>
 * Title: ITC
 * </p>
 * <p>
 * Description：项目管理控制类
 * </p>
 * <p>
 * Copyright: Copyright (c) 2012
 * </p>
 * <p>
 * Company:云恒瑞通
 * </p>
 * 
 * @author tianxu
 * @date 2012-07-09
 */
public class ProjectAction extends ActionSupport
{

    /**
     * 串码
     */
    private static final long serialVersionUID = -7246926639366568559L;

    private static final Logger log = Logger.getLogger(ProjectAction.class);
    // 操作表名
    private static final String TABLE_NAME = "T_CB_PROJECTS";

    // 项目参数
    private Project project;
    /**
     * 注入service 类
     */
    private OrganizaService organizaService;

    // 组织结构
    private List<Organiza> organizaLists;

    private String projectOrganize; // 部门科室

    private String organizeValue; // 部门科室显示的value

    private String citycode; // 获取地市的cityCode

    private String tree;
    /** 当前页 */
    int currentPage;
    /** 分页对象 */
    PageBean page;

    // 项目编号
    private String projectId;
    // 结果标识位
    private boolean rsflag;
    // 操作标识位
    private String opflag;
    // 项目媒介ID
    private int projMedId;
    // 业务逻辑类
    private ProjectService projectService;
    // 日志服务类
    private OptLogService optLogService;
    // 媒介服务类
    private MediumService mediumService;
    // 数据字典
    private List<Dictionary> typelist;
    // 数据字典
    private List<Dictionary> citylist;
    // 项目结果集合
    private List<Project> projectList;

    private String projMedForDel;

    // 项目媒介ID字符串
    private String[] projMedIds;

    // 媒介资源新状态
    private String newStatus;

    // 项目当前状态
    private String curStatus;
    //选定状态
    private String nowStatus;
    // 投放明细预占列表集合
    private List<CastForProj> curStatusList;
    // 当前状态小计
    private Double sumCount;
    // 状态小计map
    private Map<String, Double> sumMap;
    // 明细数量MAP
    private Map<String, Integer> countMap;
    // 投放明细预占列表集合
    private List<CastForProj> castPrevList;
    // 投放明细已中止列表集合
    private List<CastForProj> castSusList;
    // 投放明细占用列表集合
    private List<CastForProj> castOccList;
    // 投放明细呈批中列表集合
    private List<CastForProj> castPendList;
    // 投放明细完成投放列表集合
    private List<CastForProj> castFinPtList;
    // 投放明细完成结算列表集合
    private List<CastForProj> castFinStList;
    // 排期信息列表集合
    private List<CastForProj> arrangeList;
    // 操作日志结果集合
    private List<Log> logList;
    // 媒介详情
    private Medium medDetail;
    // 搜索类型
    private Integer searchType;
    // 搜索关键字
    private String keyword;
    // 开始时间
    private String projectStartTime;
    // 结束时间
    private String projectEndTime;
    // 排期月份
    private String arrangeTime;

    // 媒介编号
    private String mediumId;

    private Integer idForMed;

    private String flag;

    private String errorCode;

    public String getFlag()
    {
        return flag;
    }

    public void setFlag(String flag)
    {
        this.flag = flag;
    }

    // 获取查询条件MAP
    private Map<String, Object> getParamsMap(Map<String, Object> params)
    {
        UserInfo user = (UserInfo) ServletActionContext.getRequest()
                .getSession()
                .getAttribute("logUser");
        if (project == null)
        {
            project = new Project();
        }
        if (null != searchType && 1 == searchType && null != keyword
                && !"".equals(keyword))
        {
            project.setProjectName(keyword.trim());
            params.put("projectName", keyword.trim());
        }
        if (null != searchType && 2 == searchType && null != keyword
                && !"".equals(keyword))
        {
            project.setProjectManager(keyword.trim());
            params.put("projectManager", keyword.trim());
            params.put("isPersonal", "no");
        }
        if (projectStartTime != null)
        {
            params.put("projectStartTime", projectStartTime);
        }
        if (projectEndTime != null)
        {
            params.put("projectEndTime", projectEndTime);
        }

        if (!StringUtils.isEmpty(project.getProjectType()))
        {
            params.put("projectType", project.getProjectType());
        }
        if (!StringUtils.isEmpty(arrangeTime))
        {
            params.put("arrangeTime", arrangeTime + "-1");
        }

        if (null != user && null != project.getCity()
                && !"".equals(project.getCity()))
        {
            // project.setCity(user.getUsercity());
            params.put("city", project.getCity());
        }
        else
        {
            project.setCity(user.getUsercity());
            params.put("city", user.getUsercity());
        }

        // 省级
        if ("1".equals(user.getUserRole().getRoleRange()))
        {
            citylist = DatadicContext.getInstance()
                    .getDicDataListByCode(Constants.CITY_AREA_CODE);
            // params.put("city", "");
        }
        // 地市级
        else if ("2".equals(user.getUserRole().getRoleRange()))
        {
            Dictionary city = DatadicContext.getInstance()
                    .getDatadicObject(user.getUsercity(),
                            Constants.CITY_AREA_CODE);
            if (null == city)
            {
                city = new Dictionary();
                city.setId("");
                city.setDatavalue("无相关地市");
            }
            citylist = new ArrayList<Dictionary>();
            citylist.add(city);

        }
        // 个人级
        else if ("3".equals(user.getUserRole().getRoleRange()))
        {
            Dictionary city = DatadicContext.getInstance()
                    .getDatadicObject(user.getUsercity(),
                            Constants.CITY_AREA_CODE);
            if (null == city)
            {
                city = new Dictionary();
                city.setId("");
                city.setDatavalue("无相关地市");
            }
            citylist = new ArrayList<Dictionary>();
            citylist.add(city);

            if (null != searchType && 2 == searchType && null != keyword
                    && !"".equals(keyword))
            {
                project.setProjectManager(user.getUserName());
                params.put("projectManager", user.getUserName());
                params.put("isPersonal", "yes");
            }
            // params.put("", value);
        }

        // 项目类型下拉
        typelist = DatadicContext.getInstance()
                .getDicDataListByCode(Constants.PROJECT_TYPE_CLASS_CODE);
        // 组织机构查询下拉
        if (null != projectOrganize && !"".equals(projectOrganize))
        {
            String orgName = getOrganize(projectOrganize);
            organizeValue = orgName;
            if (!"--部门科室--".equals(organizeValue))
            {
                params.put("projectSection", projectOrganize);
            }
        }
        if (null == organizeValue || "".equals(organizeValue))
        {
            organizeValue = "--部门科室--";
        }
        // 查询组织架构信息 获取当前节点集合
        organizaLists = new ArrayList<Organiza>();
        organizaLists.addAll(organizaService.getOrganizaByCity(project.getCity()));
        // 获取生成组织架构字符串
        tree = Constants.getOrganizaTree(organizaLists);
        // if ("ALL".equals(project.getProjectDepartment()))
        // {
        // params.put("projectDepartment", project.getProjectDepartment());
        // }
        // if ("ALL".equals(project.getProjectSection()))
        // {
        // params.put("projectSection", project.getProjectSection());
        // }
        return params;
    }

    /**
     * @category 获取地市下的组织架构
     * @return
     */
    public String getOrganization()
    {
        // response 对象
        HttpServletResponse response = ServletActionContext.getResponse();
        PrintWriter out = null;
        try
        {
            // 获取PrintWriter对象
            ServletActionContext.getRequest()
                    .setCharacterEncoding(Constants.STR_UTF);
            response.setContentType(Constants.STR_CHARSET);
            out = response.getWriter();
            if (null != citycode && !"".equals(citycode))
            {
                String contractorCityCode = URLDecoder.decode(citycode, "UTF-8");
                // 查询组织架构信息 获取当前节点集合
                organizaLists = new ArrayList<Organiza>();
                organizaLists = organizaService.getOrganizaByCity(contractorCityCode);
                // 获取生成组织架构字符串
                String flagstr = Constants.getOrganizaTree(organizaLists);
                Map<Object, Object> map = new HashMap<Object, Object>();
                map.put("flagstr", flagstr);

                JSONArray jo = JSONArray.fromObject(map);
                out.print(jo.toString());
                out.flush();
                out.close();
            }
        }
        catch (Exception e)
        {
            log.error("获取组织机构信息失败！");
            return ERROR;
        }
        return null;
    }

    public String test()
    {
        return SUCCESS;
    }

    /**
     * @category 查询项目列表
     */

    public String queryList()
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectAction queryList() method,return String");
        }

        // Dictionary roleRange =
        // DatadicContext.getInstance().getDatadicObject(id, classCode)
        // 获取参数map

        try
        {
            Map<String, Object> params = new HashMap<String, Object>();

            params = getParamsMap(params);
            // 初始化查询
            page = new PageBean(currentPage == 0 ? 1 : currentPage,
                    Constants.PAGE_SIZE);
            projectList = getProjectService().getProjectList(params, page);

            // /recordNum = projectList.size();
            if (projectList != null && !projectList.isEmpty())
            {
                for (Project proj : projectList)
                {
                    // 项目类型
                    Dictionary dic = DatadicContext.getInstance()
                            .getDatadicObject(proj.getProjectType(),
                                    Constants.PROJECT_TYPE_CLASS_CODE);
                    if (dic != null)
                    {
                        proj.setProjectType(dic.getDatavalue());
                    }

                    // 归属地市
                    dic = DatadicContext.getInstance()
                            .getDatadicObject(proj.getCity(),
                                    Constants.CITY_AREA_CODE);
                    if (dic != null)
                    {
                        proj.setCity(dic.getDatavalue());
                    }

                    // proj.setArrangeTime(arrangeTime);
                    if (null != arrangeTime)
                    {
                        proj.setArrangeTime(StringUtils.string2Date(arrangeTime,
                                "yyyy-M"));
                    }
                    // 查询项目状态
                    String proStatus = getProjectService().getProjCurStatus(proj.getProjectId());

                    if (null != proStatus && !"".equals(proStatus))
                    {
                        dic = DatadicContext.getInstance()
                                .getDatadicObject(proStatus,
                                        Constants.MEDIUM_STATE_CODE);

                        proj.setProCurStatus(proStatus);
                        proj.setProStatusName(dic.getDatavalue());
                    }
                    else
                    {
                        proj.setProCurStatus("");
                        proj.setProStatusName("");
                    }

                }
            }
        }
        catch (Exception e)
        {
            log.error("查询项目列表信息失败！");
            return ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectAction queryList() method,return String");
        }

        return "querylist";
    }

    /**
     * @category 查看项目信息
     * @exception
     * @return
     */
    public String detail()
    {

        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectAction detail() method,return String");
        }
        try
        {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("projectId", projectId);

            project = getProjectService().getProjectRelateInfo(params);

            if (null == project || project.getProjectId() == null
                    || "".equals(project.getProjectId()))
            {
                // throw new ITCException("3004");
                // 打印方法体异常日志
                log.error("项目相关信息不存在！项目编号： " + projectId);
                errorCode = "3004";
                return "errorPage";
            }

            // 项目类型
            Dictionary dic = DatadicContext.getInstance()
                    .getDatadicObject(project.getProjectType(),
                            Constants.PROJECT_TYPE_CLASS_CODE);
            if (dic != null)
            {
                project.setProjectType(dic.getDatavalue());
            }

            // 归属地市
            dic = DatadicContext.getInstance()
                    .getDatadicObject(project.getCity(),
                            Constants.CITY_AREA_CODE);
            if (dic != null)
            {
                project.setCity(dic.getDatavalue());
            }

            // 查询相关投放明细信息
            curStatus = getProjectService().getProjCurStatus(projectId);
            if (null == curStatus)
            {
                curStatus = "";
            }

            countMap = getProjectService().getCastCountByProjId(projectId,
                    curStatus);
            Map<String, List<CastForProj>> castListMap = getProjectService().getCastByProjId(projectId,
                    curStatus);
            sumMap = new HashMap<String, Double>();
            if (castListMap.get("1") != null)
            {
                castPrevList = castListMap.get("1");
                Double sum = 0.0;
                for (CastForProj cast : castPrevList)
                {
                    sum = Arith.add(sum, cast.getMediumTotalPrice());
                }
                sumMap.put("1", sum);
            }
            if (castListMap.get("2") != null)
            {
                castPendList = castListMap.get("2");
                Double sum = 0.0;
                for (CastForProj cast : castPendList)
                {
                    sum = Arith.add(sum, cast.getMediumTotalPrice());
                }
                sumMap.put("2", sum);
            }
            if (castListMap.get("3") != null)
            {
                castOccList = castListMap.get("3");
                Double sum = 0.0;
                for (CastForProj cast : castOccList)
                {
                    sum = Arith.add(sum, cast.getMediumTotalPrice());
                }
                sumMap.put("3", sum);
            }
            if (castListMap.get("4") != null)
            {
                castFinPtList = castListMap.get("4");
                Double sum = 0.0;
                for (CastForProj cast : castFinPtList)
                {
                    sum = Arith.add(sum, cast.getMediumTotalPrice());
                }
                sumMap.put("4", sum);
            }
            if (castListMap.get("5") != null)
            {
                castFinStList = castListMap.get("5");
                Double sum = 0.0;
                for (CastForProj cast : castFinStList)
                {
                    sum = Arith.add(sum, cast.getMediumTotalPrice());
                }
                sumMap.put("5", sum);
            }
            if (castListMap.get("6") != null)
            {
                castSusList = castListMap.get("6");
                Double sum = 0.0;
                for (CastForProj cast : castSusList)
                {
                    sum = Arith.add(sum, cast.getMediumTotalPrice());
                }
                sumMap.put("6", sum);
            }

            // Dictionary state = null;
            // List<CastForProj> casts = null;
            // List<CastForProj> arranges = null;
            // DateFormat format = new SimpleDateFormat("yyyy-MM-dd");

            // arrangeList =
            // getProjectService().getCastArrangeByProjId(projectId);
            //            
            // if(null!=arrangeList && !arrangeList.isEmpty())
            // {
            // for (int i = 0; i < arrangeList.size(); i++)
            // {
            // state = DatadicContext.getInstance()
            // .getDatadicObject(arrangeList.get(i).getMediumStatus(),
            // Constants.RES_STATE_CODE);
            // arrangeList.get(i).setMediumStatusName(state.getDatavalue());
            //                    
            // arrangeList.get(i).setGroupKey(format.format(arrangeList.get(i).getArrangeTime())+"_"+arrangeList.get(i).getMediumStatus());
            // }

            // castInfoList = getProjectService().getCastByProjId(projectId);
            //            
            // if(null!=castInfoList && !castInfoList.isEmpty())
            // {
            // for(int j = 0; j < castInfoList.size(); j++)
            // {
            // castInfoList.get(j).setGroupKey(castInfoList.get(j).getMediumStatus());
            // }
            // }
            // }

            // 查询操作记录日志
            Log loger = new Log();
            loger.setLogType(TABLE_NAME);
            loger.setRelationId(project.getProjectId());
            logList = optLogService.getLogsBySample(loger);
        }
        catch (Exception e)
        {
            // 异常日志
            log.error("Error ProjectAction detail() method,return String", e);
            // e.printStackTrace();
            return ERROR;

        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectAction detail() method,return String");
        }
        return "detail";
    }

    /**
     * @category 新增初始化
     * @return
     */
    public String addInit()
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectAction addInit() method,return String");
        }

        try
        {
            typelist = DatadicContext.getInstance()
                    .getDicDataListByCode(Constants.PROJECT_TYPE_CLASS_CODE);
            citylist = DatadicContext.getInstance()
                    .getDicDataListByCode(Constants.CITY_AREA_CODE);

            // 查询组织架构信息 获取当前节点集合
            List<Organiza> organizaLists = new ArrayList<Organiza>();

            organizaLists = organizaService.getOrganizaByCity("DG");

            // 获取生成组织架构字符串
            flag = Constants.getOrganizaTree(organizaLists);
        }
        catch (Exception e)
        {
            // 打印方法体异常日志
            // log.error("Error ProjectAction addInit() method,return String ",
            // e);
            // errorCode = e.getCode();
            // return "errorPage";
            // 异常日志
            log.error("Error ProjectAction addInit() method,return String", e);
            // e.printStackTrace();
            return ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectAction addInit() method,return String ");
        }
        return "addPage";
    }

    /**
     * @category 新增项目
     * @return
     */
    public String add()
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectAction add() method,return String");
        }
        UserInfo user = (UserInfo) ActionContext.getContext()
                .getSession()
                .get("logUser");
        // 加入新增项目编号方法
        try
        {
            project.setProjectId(project.getProjectId().trim());
            project.setProjectName(project.getProjectName().trim());
            project.setProjectEndTime(StringUtils.string2Date(projectEndTime,
                    "yyyy-MM-dd"));
            project.setProjectStartTime(StringUtils.string2Date(projectStartTime,
                    "yyyy-MM-dd"));
            // project.setProjectDepartment("");
            project.setArrangeTime(StringUtils.string2Date(arrangeTime,
                    "yyyy-MM"));

            // 组织
            project.setProjectSection(user.getBlongOrgCode());
            project.setProjectDepartment(user.getBlongOrgName());
            // 负责人
            project.setProjectManager(user.getUserCode());
            // 城市编号
            project.setCity(user.getUsercity() == null ? ""
                    : user.getUsercity());
            project.setProjectDesciption(project.getProjectDesciption().trim());
            getProjectService().addProjectInfo(project);
            // project.setProjectId(projId);
            // 操作日志记录
            // UserInfo user =
            // (UserInfo)ServletActionContext.getRequest().getSession().getAttribute("logUser");
            Timestamp time = new Timestamp(System.currentTimeMillis());
            Date data = new Date(System.currentTimeMillis());
            String datatime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
            System.out.println("data======" + data);
            StringBuffer contents = new StringBuffer();

            if (null != user)
            {
                contents.append(time)
                        .append(" ")
                        .append(user.getCityName())
                        .append(user.getOrganiza().getOrgName())
                        .append(user.getUserName())
                        .append("创建项目。");
            }
            else
            {
                contents.append(time)
                        .append(" ")
                        .append("全省")
                        .append("总部")
                        .append("管理员")
                        .append("创建项目。");
            }
            /** 获取变更记录的数据 */
            Log logRecord = new Log(TABLE_NAME,
                    contents.toString(),
                    project.getProjectId(),
                    datatime,
                    user.getUserName(),
                    user.getUsercity(),
                    user.getOrganiza().getOrgName(),
                    "科室");
            optLogService.addLog(logRecord);

        }
        catch (Exception e)
        {
            // 打印方法体异常日志
            // if ("3001".equals(e.getCode()))
            // {
            // log.error("Error ProjectAction add() method,return String ", e);
            // errorCode = e.getCode();
            // return "errorPage";
            // }
            // 异常日志
            log.error("Error ProjectAction add() method,return String", e);
            // e.printStackTrace();
            return ERROR;

        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectAction add() method,return String ");
        }
        return SUCCESS;
    }

    /**
     * @category 修改项目信息
     * @return
     */
    public String mod()
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectAction mod() method,return String");
        }
        UserInfo user = (UserInfo) ActionContext.getContext()
                .getSession()
                .get("logUser");
        try
        {
            project.setProjectId(project.getProjectId().trim());
            project.setProjectName(project.getProjectName().trim());
            project.setProjectEndTime(StringUtils.string2Date(projectEndTime,
                    "yyyy-MM-dd"));
            project.setProjectStartTime(StringUtils.string2Date(projectStartTime,
                    "yyyy-MM-dd"));
            // project.setProjectDepartment("");
            project.setArrangeTime(StringUtils.string2Date(arrangeTime,
                    "yyyy-MM"));
            project.setProjectDesciption(project.getProjectDesciption().trim());
            rsflag = getProjectService().modProjectInfo(project);

            // 操作日志记录
            // UserInfo user =
            // (UserInfo)ServletActionContext.getRequest().getSession().getAttribute("logUser");
            Timestamp time = new Timestamp(System.currentTimeMillis());
            Date data = new Date(System.currentTimeMillis());
            String datatime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
            System.out.println("data======" + data);
            StringBuffer contents = new StringBuffer();

            if (null != user)
            {
                contents.append(time)
                        .append(" ")
                        .append(user.getCityName())
                        .append(user.getOrganiza().getOrgName())
                        .append(user.getUserName())
                        .append("修改项目。");
            }
            else
            {
                contents.append(time)
                        .append(" ")
                        .append("全省")
                        .append(" ")
                        .append("总部")
                        .append(" ")
                        .append("管理员")
                        .append(" ")
                        .append("修改项目。");
            }
            /** 获取变更记录的数据 */
            Log logRecord = new Log(TABLE_NAME,
                    contents.toString(),
                    project.getProjectId(),
                    datatime,
                    user.getUserName(),
                    user.getUsercity(),
                    user.getOrganiza().getOrgName(),
                    "科室");
            optLogService.addLog(logRecord);

            if (rsflag)
            {
                this.alertMessage("项目修改成功", "proQuery.action?opflag=init");
            }

        }
        catch (Exception e)
        {
            // 打印方法体异常日志
            // if ("3002".equals(e.getCode()))
            // {
            // log.error("Error ProjectAction mod() method,return String ", e);
            // errorCode = e.getCode();
            // return ERROR;
            // }
            // 异常日志
            log.error("Error ProjectAction mod() method,return String", e);
            // e.printStackTrace();
            return ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectAction mod() method,return String ");
        }
        return null;
    }

    /**
     * @category 修改初始化
     * @return
     */
    public String modInit()
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectAction modInit() method,return String");
        }

        try
        {
            typelist = DatadicContext.getInstance()
                    .getDicDataListByCode(Constants.PROJECT_TYPE_CLASS_CODE);
            citylist = DatadicContext.getInstance()
                    .getDicDataListByCode(Constants.CITY_AREA_CODE);

            // 查询项目详细信息
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("projectId", projectId);
            project = getProjectService().getProjectRelateInfo(params);
            if (project == null || project.getProjectId() == null
                    || "".equals(project.getProjectId()))
            {
                // 打印方法体异常日志
                log.info("要修改的项目信息不存在或已删除，项目编号： " + projectId);
                errorCode = "3004";
                return "errorPage";
            }
            // 查询组织架构信息 获取当前节点集合
            List<Organiza> organizaLists = new ArrayList<Organiza>();

            organizaLists = organizaService.getOrganizaByCity("DG");

            // 获取生成组织架构字符串
            flag = Constants.getOrganizaTree(organizaLists);

        }
        catch (Exception e)
        {
            // 异常日志
            log.error("Error ProjectAction modInit() method,return String", e);
            // e.printStackTrace();
            return ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectAction modInit() method,return String ");
        }
        return "mod";
    }

    /**
     * @category 删除项目
     * @return
     */
    public String del()
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectAction delete() method,return String");
        }

        try
        {
            rsflag = getProjectService().delProjectInfo(projectId);
        }
        catch (ITCException t)
        {
            if ("3003".equals(t.getCode()))
            {
                log.error("Error ProjectAction del() method,return String ", t);
                errorCode = t.getCode();
                return "errorPage";
            }
        }
        catch (Exception e)
        {
            // 打印方法体异常日志
            // if ("3003".equals(e.getCode()))
            // {
            // log.error("Error ProjectAction del() method,return String ", e);
            // errorCode = e.getCode();
            // return "errorPage";
            // }
            // 异常日志
            log.error("Error ProjectAction del() method,return String", e);
            // e.printStackTrace();
            return ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectAction delete() method,return String ");
        }
        return SUCCESS;
    }

    // public String delCast()
    // {
    // // 打印进入方法体日志
    // if (log.isInfoEnabled())
    // {
    // log.info("Enter ProjectAction delCast() method,return String");
    // }
    // try
    // {
    // rsflag =
    // getProjectService().deleteCastInfo(Integer.valueOf(projMedForDel));
    // //projectId = projectId;
    // }
    // catch (Exception e)
    // {
    // log.error("Error ProjectAction delCast() method,return String", e);
    // // e.printStackTrace();
    // return ERROR;
    // }
    // // 打印退出方法体日志
    // if (log.isInfoEnabled())
    // {
    // log.info("Exit ProjectAction delCast() method,return String ");
    // }
    // return SUCCESS;
    // }

    public String castDetailForList()
    {
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectAction castDetailForList() method,return String");
        }
        try
        {
            // 查询相关投放明细
            Map<String, List<CastForProj>> castListMap = getProjectService().getCastByProjId(projectId,
                    curStatus);

            if (castListMap.get("1") != null && "1".equals(curStatus))
            {
                curStatusList = castListMap.get("1");
                Double sum = 0.0;
                for (CastForProj cast : castListMap.get("1"))
                {
                    sum = Arith.add(sum, cast.getMediumTotalPrice());
                }
                sumCount = sum;
            }
            if (castListMap.get("2") != null && "2".equals(curStatus))
            {
                curStatusList = castListMap.get("2");
                Double sum = 0.0;
                for (CastForProj cast : castListMap.get("2"))
                {
                    sum = Arith.add(sum, cast.getMediumTotalPrice());
                }
                sumCount = sum;
            }
            if (castListMap.get("3") != null && "3".equals(curStatus))
            {
                curStatusList = castListMap.get("3");
                Double sum = 0.0;
                for (CastForProj cast : castListMap.get("3"))
                {
                    sum = Arith.add(sum, cast.getMediumTotalPrice());
                }
                sumCount = sum;
            }
            if (castListMap.get("4") != null && "4".equals(curStatus))
            {
                curStatusList = castListMap.get("4");
                Double sum = 0.0;
                for (CastForProj cast : castListMap.get("4"))
                {
                    sum = Arith.add(sum, cast.getMediumTotalPrice());
                }
                sumCount = sum;
            }
            if (castListMap.get("5") != null && "5".equals(curStatus))
            {
                curStatusList = castListMap.get("5");
                Double sum = 0.0;
                for (CastForProj cast : castListMap.get("5"))
                {
                    sum = Arith.add(sum, cast.getMediumTotalPrice());
                }
                sumCount = sum;
            }
            if (castListMap.get("6") != null && "6".equals(curStatus))
            {
                curStatusList = castListMap.get("6");
                Double sum = 0.0;
                for (CastForProj cast : castListMap.get("6"))
                {
                    sum = Arith.add(sum, cast.getMediumTotalPrice());
                }
                sumCount = sum;
            }
        }
        catch (Exception ex)
        {
            log.error("Error ProjectAction castDetailForList() method,return String",
                    ex);
            return ERROR;
        }
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectAction castDetailForList() method,return String ");
        }

        return SUCCESS;
    }

    public String modCastStatus()
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectAction modCastStatus() method,return String");
        }
        try
        {
            List<Integer> proMedIdList = new ArrayList<Integer>();
            for (String proMdId : projMedIds)
            {
                proMedIdList.add(Integer.valueOf(proMdId));
            }
            rsflag = getProjectService().updateCastStatus(newStatus,
                    proMedIdList);

            // projectId = projectId;
        }
        catch (Exception e)
        {
            log.error("Error ProjectAction modCastStatus() method,return String",
                    e);
            // e.printStackTrace();
            return ERROR;
        }
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectAction modCastStatus() method,return String ");
        }
        if ("list".equals(opflag))
        {
            return "querylist";
        }
        return SUCCESS;
    }

    public String detailForMedium()
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ProjectAction detailForCast() method,return String");
        }
        try
        {
           medDetail = mediumService.findMediumById(idForMed.intValue());
           Map<String,Object> params = new HashMap<String,Object>();
           Integer medAmount = null;
           if("".equals(curStatus))
           {
               params.put("projMedId", projMedId);
               medAmount = getProjectService().getAmountByCur(params);
           }
           else if(null!=curStatus && null!=nowStatus && curStatus.equals(nowStatus))
           {
               params.put("projMedId", projMedId);
               medAmount = getProjectService().getAmountByCur(params);
           }
           else if(!curStatus.equals(nowStatus))
           {
               params.put("projMedId", projMedId);
               params.put("nowStatus", nowStatus);
               medAmount = getProjectService().getAmountByCur(params);
           }
           
           if(null!=medAmount)
           {
               medDetail.setMediumAbility(medAmount);
           }
           //medDetail.getMediasumm().setMediaName(medDetail.getMediasumm().getMediaName()==null?"":medDetail.getMediasumm().getMediaName());
           medDetail.setMediumStandard(medDetail.getMediumStandard()==null?"":medDetail.getMediumStandard());
           medDetail.setMediumStyle(medDetail.getMediumStyle()==null?"":medDetail.getMediumStyle());
           medDetail.setMediumColumn(medDetail.getMediumColumn()==null?"":medDetail.getMediumColumn());
           medDetail.setMediumPage(medDetail.getMediumPage()==null?"":medDetail.getMediumPage());
           medDetail.setMediumPosition(medDetail.getMediumPosition()==null?"":medDetail.getMediumPosition());
        }
        catch(Exception ex)
        {
            log.error("Error ProjectAction detailForCast() method,return String",
                    ex);
            // e.printStackTrace();
            return ERROR;   
        }
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit ProjectAction detailForCast() method,return String");
        }
        return SUCCESS;
    }

    public String getOrganize(String organizaListsid)
    {
        List<Organiza> organizaL = new ArrayList<Organiza>();
        Organiza organiza = new Organiza();
        organiza.setOrgCode(organizaListsid);
        // 查询组织架构信息 获取当前节点集合
        organizaL = organizaService.getOrganizas(organiza);
        String orgName = organizaL.get(0).getOrgName();
        return orgName;
    }

    /**
     * @category 项目是否已存在
     * @return
     * @throws UnsupportedEncodingException
     */
    public String isExist() throws UnsupportedEncodingException
    {

        // 查询项目详细信息
        Map<String, Object> params = new HashMap<String, Object>();
        if (!StringUtils.isEmpty(project.getProjectId()))
        {
            params.put("projectId", project.getProjectId());
        }

        if (!StringUtils.isEmpty(project.getProjectName()))
        {
            params.put("projectName", project.getProjectName());
        }

        project = getProjectService().getProjectRelateInfo(params);

        if (project == null)
        {
            rsflag = false;
        }
        else
        {
            rsflag = true;
        }
        return SUCCESS;
    }

    private void alertMessage(String message, String url)
    {
        try
        {
            HttpServletResponse response = ServletActionContext.getResponse();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html; charset=utf-8");
            PrintWriter out = response.getWriter();
            out = response.getWriter();
            out.println(" <script type='text/javascript' >");
            out.println("alert('" + message + "');");
            out.println("location='" + url + "'");
            out.println(" </script>");
            out.flush();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    public void validateExecute()
    {

    }

    @Override
    public void validate()
    {

    }

    public boolean isRsflag()
    {
        return rsflag;
    }

    public void setRsflag(boolean rsflag)
    {
        this.rsflag = rsflag;
    }

    public String getOpflag()
    {
        return opflag;
    }

    public void setOpflag(String opflag)
    {
        this.opflag = opflag;
    }

    public String getProjectId()
    {
        return projectId;
    }

    public void setProjectId(String projectId)
    {
        this.projectId = projectId;
    }

    public ProjectService getProjectService()
    {
        return projectService;
    }

    public void setProjectService(ProjectService projectService)
    {
        this.projectService = projectService;
    }

    public Project getProject()
    {
        return project;
    }

    public void setProject(Project project)
    {
        this.project = project;
    }

    public PageBean getPage()
    {
        return page;
    }

    public void setPage(PageBean page)
    {
        this.page = page;
    }

    public int getCurrentPage()
    {
        return currentPage;
    }

    public void setCurrentPage(int currentPage)
    {
        this.currentPage = currentPage;
    }

    public Integer getSearchType()
    {
        return searchType;
    }

    public void setSearchType(Integer searchType)
    {
        this.searchType = searchType;
    }

    public String getKeyword()
    {
        return keyword;
    }

    public void setKeyword(String keyword)
    {
        this.keyword = keyword;
    }

    public List<Project> getProjectList()
    {
        return projectList;
    }

    public void setProjectList(List<Project> projectList)
    {
        this.projectList = projectList;
    }

    public List<Dictionary> getTypelist()
    {
        return typelist;
    }

    public void setTypelist(List<Dictionary> typelist)
    {
        this.typelist = typelist;
    }

    public List<Dictionary> getCitylist()
    {
        return citylist;
    }

    public void setCitylist(List<Dictionary> citylist)
    {
        this.citylist = citylist;
    }

    public String getProjectStartTime()
    {
        return projectStartTime;
    }

    public void setProjectStartTime(String projectStartTime)
    {
        this.projectStartTime = projectStartTime;
    }

    public String getProjectEndTime()
    {
        return projectEndTime;
    }

    public void setProjectEndTime(String projectEndTime)
    {
        this.projectEndTime = projectEndTime;
    }

    public String getArrangeTime()
    {
        return arrangeTime;
    }

    public void setArrangeTime(String arrangeTime)
    {
        this.arrangeTime = arrangeTime;
    }

    public OptLogService getOptLogService()
    {
        return optLogService;
    }

    public void setOptLogService(OptLogService optLogService)
    {
        this.optLogService = optLogService;
    }

    public OrganizaService getOrganizaService()
    {
        return organizaService;
    }

    public void setOrganizaService(OrganizaService organizaService)
    {
        this.organizaService = organizaService;
    }

    public List<Log> getLogList()
    {
        return logList;
    }

    public void setLogList(List<Log> logList)
    {
        this.logList = logList;
    }

    public List<Organiza> getOrganizaLists()
    {
        return organizaLists;
    }

    public void setOrganizaLists(List<Organiza> organizaLists)
    {
        this.organizaLists = organizaLists;
    }

    public String getProjectOrganize()
    {
        return projectOrganize;
    }

    public void setProjectOrganize(String projectOrganize)
    {
        this.projectOrganize = projectOrganize;
    }

    public String getOrganizeValue()
    {
        return organizeValue;
    }

    public void setOrganizeValue(String organizeValue)
    {
        this.organizeValue = organizeValue;
    }

    public String getTree()
    {
        return tree;
    }

    public void setTree(String tree)
    {
        this.tree = tree;
    }

    public String getErrorCode()
    {
        return errorCode;
    }

    public void setErrorCode(String errorCode)
    {
        this.errorCode = errorCode;
    }

    public String getCitycode()
    {
        return citycode;
    }

    public void setCitycode(String citycode)
    {
        this.citycode = citycode;
    }

    public List<CastForProj> getArrangeList()
    {
        return arrangeList;
    }

    public void setArrangeList(List<CastForProj> arrangeList)
    {
        this.arrangeList = arrangeList;
    }

    public int getProjMedId()
    {
        return projMedId;
    }

    public void setProjMedId(int projMedId)
    {
        this.projMedId = projMedId;
    }

    public MediumService getMediumService()
    {
        return mediumService;
    }

    public void setMediumService(MediumService mediumService)
    {
        this.mediumService = mediumService;
    }

    public String getMediumId()
    {
        return mediumId;
    }

    public void setMediumId(String mediumId)
    {
        this.mediumId = mediumId;
    }

    public String getCurStatus()
    {
        return curStatus;
    }

    public void setCurStatus(String curStatus)
    {
        this.curStatus = curStatus;
    }

    public Map<String, Integer> getCountMap()
    {
        return countMap;
    }

    public void setCountMap(Map<String, Integer> countMap)
    {
        this.countMap = countMap;
    }

    public List<CastForProj> getCastPrevList()
    {
        return castPrevList;
    }

    public void setCastPrevList(List<CastForProj> castPrevList)
    {
        this.castPrevList = castPrevList;
    }

    public List<CastForProj> getCastSusList()
    {
        return castSusList;
    }

    public void setCastSusList(List<CastForProj> castSusList)
    {
        this.castSusList = castSusList;
    }

    public List<CastForProj> getCastOccList()
    {
        return castOccList;
    }

    public void setCastOccList(List<CastForProj> castOccList)
    {
        this.castOccList = castOccList;
    }

    public List<CastForProj> getCastPendList()
    {
        return castPendList;
    }

    public void setCastPendList(List<CastForProj> castPendList)
    {
        this.castPendList = castPendList;
    }

    public List<CastForProj> getCastFinPtList()
    {
        return castFinPtList;
    }

    public void setCastFinPtList(List<CastForProj> castFinPtList)
    {
        this.castFinPtList = castFinPtList;
    }

    public List<CastForProj> getCastFinStList()
    {
        return castFinStList;
    }

    public void setCastFinStList(List<CastForProj> castFinStList)
    {
        this.castFinStList = castFinStList;
    }

    public String getNewStatus()
    {
        return newStatus;
    }

    public void setNewStatus(String newStatus)
    {
        this.newStatus = newStatus;
    }

    public Map<String, Double> getSumMap()
    {
        return sumMap;
    }

    public void setSumMap(Map<String, Double> sumMap)
    {
        this.sumMap = sumMap;
    }

    public String getProjMedForDel()
    {
        return projMedForDel;
    }

    public void setProjMedForDel(String projMedForDel)
    {
        this.projMedForDel = projMedForDel;
    }

    public String[] getProjMedIds()
    {
        return projMedIds;
    }

    public void setProjMedIds(String[] projMedIds)
    {
        this.projMedIds = projMedIds;
    }

    public Integer getIdForMed()
    {
        return idForMed;
    }

    public void setIdForMed(Integer idForMed)
    {
        this.idForMed = idForMed;
    }

    public List<CastForProj> getCurStatusList()
    {
        return curStatusList;
    }

    public void setCurStatusList(List<CastForProj> curStatusList)
    {
        this.curStatusList = curStatusList;
    }

    public Double getSumCount()
    {
        return sumCount;
    }

    public void setSumCount(Double sumCount)
    {
        this.sumCount = sumCount;
    }

    public Medium getMedDetail()
    {
        return medDetail;
    }

    public void setMedDetail(Medium medDetail)
    {
        this.medDetail = medDetail;
    }

    public String getNowStatus()
    {
        return nowStatus;
    }

    public void setNowStatus(String nowStatus)
    {
        this.nowStatus = nowStatus;
    }

}
